﻿using System.Linq;
using System.Security.Principal;
using ActivateYourGlutes.Data;
using ActivateYourGlutes.Data.Model;

namespace ActivateYourGlutes.Services.Implementation
{
    public class InternalAccountProvider : ProviderBase<Account>
    {
        public InternalAccountProvider(IEntityModelFactory factory) : base(factory)
        {
        }

        /// <summary>
        /// TODO:
        /// This is nasty but in one instance during controller initialisation I need access to the native
        /// account information in LocaleFactory.cs. Can be solved by pushing the LocaleFactory down here.
        /// </summary>
        /// <param name="user"></param>
        /// <returns></returns>
        public Account InternalCurrentAccount(IPrincipal user)
        {
            var username = user.Identity.Name;
            var account = AccountRepositoryExtensions.FullEntities(Repository).
                Where(ac => ac.Username == username).FirstOrDefault();

            return account;
        }
    }
}
